package com.zcx.net;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zcx.R;
import com.zcx.app.AppData;
import com.zcx.app.AppManager;
import com.zcx.app.DollApplication;
import com.zcx.base.BaseBean;
import com.zcx.common.Helper;

/**
 * Created by hp on 2017/9/22.
 */

public class BaseCallback extends StringCallback {

    public static final int NO_FULL = -10;
    public static final int OTHER = -2;
    public static final int NO_WORK = -5;

    private BaseInterface baseInterface;

    public BaseCallback(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    @Override
    public void onError(Response<String> response) {
        if (null != baseInterface) {
            if (null != response) {
                if (isLogin(response)) {
                    baseInterface.onError(NO_WORK, ResourceHelper.getString(R.string.no_network));
                    return;
                }
                baseInterface.onError(NetWorkHelper.isNetworkAvailable() ? OTHER : NO_WORK, "");
            } else {
                baseInterface.onError(NetWorkHelper.isNetworkAvailable() ? OTHER : NO_WORK, ResourceHelper.getString(R.string.no_network));
            }
        } else {
            baseInterface.onError(NO_WORK, ResourceHelper.getString(R.string.no_network));
        }
    }

    @Override
    public void onSuccess(Response<String> response) {
        if (null != baseInterface && null != response) {
            if (isLogin(response)) {
                baseInterface.onError(NO_WORK, ResourceHelper.getString(R.string.no_network));
                return;
            }
            if (!TextUtils.isEmpty(response.body())) {
                try {
                    BaseBean baseBean = JSON.parseObject(response.body(), BaseBean.class);
                    if (Helper.isNotNull(baseBean)) {
                        switch (baseBean.getError()) {

                            case AppData.UrlType.YES:
                                baseInterface.onSuccess(baseBean);
                                return;

                            case AppData.UrlType.NO_COIN:
                                baseInterface.onError(baseBean.getError(), baseBean.getMessage());
                                break;

                            case AppData.UrlType.NO_LOGIN:
                            case AppData.UrlType.SYSTEM:
                                baseInterface.onError(baseBean.getError(), "");
                                return;

                        }
                    }
                    baseInterface.onError(baseBean.getError(), baseBean.getMessage());
                } catch (Exception e) {
                    baseInterface.onError(NO_FULL, "");
                }
            } else {
                baseInterface.onError(NO_FULL, "");
            }
        } else {
            baseInterface.onError(NO_WORK, ResourceHelper.getString(R.string.no_network));
        }
    }

    private boolean isLogin(Response<String> response) {
        if (401 == response.code() && Helper.isNotNull(AppManager.getAppManager().currentActivity())) {
            return true;
        } else if (404 == response.code() && Helper.isNotNull(AppManager.getAppManager().currentActivity())) {
            if (!DollApplication.isSystem) {
                DollApplication.isSystem = true;
            }
            return true;
        } else {
            return false;
        }
    }


    public interface BaseInterface {
        void onError(int error, String message);
        void onSuccess(BaseBean baseBean);
    }

}
